package com.situ.mall.controller;



import com.situ.common.util.PageResult;
import com.situ.common.util.Result;
import com.situ.mall.pojo.entity.Product;
import com.situ.mall.pojo.entity.User;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.pojo.vo.CartVO;
import com.situ.mall.pojo.vo.ProductVO;
import com.situ.mall.service.ICartService;
import com.situ.mall.service.IProductService;
import com.situ.mall.recommend.ProductRecommendationService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private ICartService cartService;
    @Autowired
    private ProductRecommendationService productRecommendationService;

    @RequestMapping("/listPortal")
    @ResponseBody
    public PageResult listPortal(ProductQuery productQuery) {
        PageResult pageInfo = productService.listPortal(productQuery);
        return pageInfo;
    }
    @RequestMapping("/selectByProductId")
    @ResponseBody
    public Result selectByProductId(Integer id) {
        Product product = productService.selectByProductId(id);
        return Result.ok(product);
    }
    @RequestMapping("/productByPatientId")
    @ResponseBody
    public Result ProductByPatientId(Integer parentId) {
        List<Product> list = productService.ProductByPatientId(parentId);
        return Result.ok(list);
    }
    @RequestMapping("/checkedStock")
    @ResponseBody
    public Result checkedStock(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        List<CartVO> cartVOList = cartService.cartListSelectByIdChecked(userId);
        for (CartVO cartVO : cartVOList) {
            Product product = productService.selectByProductId(cartVO.getProductId());
            if (product.getStock() <cartVO.getQuantity()) {
                return Result.error(product.getName()+"库存数量不足！");
            }
        }
        return Result.ok("库存充足");
    }
    @RequestMapping("/checkStock")
    @ResponseBody
    public Result checkStock(Integer id) {

        Product product = productService.selectByProductId(id);
        if (product.getStock() <= 0) {
            return Result.error(product.getName()+"已无库存 ！");
        }

        return Result.ok("库存充足");
    }
    @RequestMapping("/selectList")
    @ResponseBody
    public Result selectList() {
        List<Product> list = productService.selectList();
        return Result.ok(list);
    }

    @RequestMapping("/recommendedProducts")
    @ResponseBody
    public Result recommendedProducts(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        Integer numberOfRecommendations = 10;
        List<ProductVO> list = productRecommendationService.recommendProductsForUser(userId, numberOfRecommendations);
        return Result.ok(list);
    }
}
